package com.bbcare.cp.plat.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

import com.bbcare.comm.AppException;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ToolUtil;
import com.bbcare.cp.plat.service.ISmsService;
import com.bbcare.cp.plat.store.ibatis.sms.ISmsRdDao;

/**
 *
 * @name:         SmsServiceImpl.java
 * @author:       tao
 * @createTime:   2018年5月9日
 * @description:  短信相关处理类
 * @modify:
 *
 */
@Service
public class SmsServiceImpl implements ISmsService{
    private final Log logger = LogFactory.getLog(SmsServiceImpl.class);

    @Autowired
    private ISmsRdDao smsRdDao;

	@Resource(name = "jedisPool")
	private JedisPool jedisPool;

	/* (non-Javadoc)
	 * @see com.bbcare.cp.plat.service.ISmsService#checkVerifyCode(java.util.Map)
	 */
	@Override
	public int checkVerifyCode(Map<String, Object> map) throws Exception {

		String verifyCode = ToolUtil.getStringFromMap(map,"verifyCode");
		String phoneNo = ToolUtil.getStringFromMap(map,"phoneNo");
		String smsType = ToolUtil.getStringFromMap(map,"smsType");
		map.put("verifyCode", verifyCode);
		map.put("smsType", smsType);
		map.put("minute", 10);
		map.put("phoneNo", phoneNo);
		// 对验证码验证
		int flag = 0 ;
		List<Map<String, Object>> lists = smsRdDao.selectVerifyCode(map);
		if (lists.size() > 0) {
			flag = 0;
			// 将短信验证码置为已校验
			map.put("verifyStatus", "02");
			smsRdDao.updateSms(map);
			return flag;
		} else {
			// 将短信验证码置为已校验
			map.remove("verifyCode");
			List<Map<String, Object>> listone = smsRdDao.selectVerifyCode(map);
			if (listone != null && listone.size() > 0) {
				String verify_code = listone.get(0).get("verify_code").toString();
				Jedis jedis = null;
				try {
					jedis = jedisPool.getResource();
					String code = jedis.get(verify_code);
					int inter = 0;
					if (code == null) {
						jedis.set(verify_code, "1");
					} else {
						inter = Integer.parseInt(code);
					}
					logger.info("inter~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + inter);
					if (inter == 5) {
						jedis.del(verify_code);
						map.put("minute", 1440);
						map.put("verifyStatus", "03");
						smsRdDao.updateSms(map);
						return flag;
					} else {
						String str = inter + 1 + "";
						jedis.set(verify_code, str);
						throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
								"尊敬的用户，您的验证码输入错误，请核对!"));
					}

				} catch (JedisConnectionException jex) {
					logger.error("redis连接异常..." + jex.getMessage());
					throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_ERROR, "Redis缓存连接失败!"));
				} finally {
					if (null != jedis) {
						try {
							jedis.close();
							logger.warn("close redis connection...");
						} catch (Exception ex) {
							ex.printStackTrace();
							throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
									"Redis缓存关闭失败!"));
						}
					}
				}
			} else {
				throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_REDIS_CONNECT_CLOSE_FAILED,
						"尊敬的用户，您的验证码已经失效，请重新获取!"));
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.bbcare.cp.plat.service.ISmsService#saveSmsInfo(java.util.Map)
	 */
	@Override
	public Map<String, Object> saveSmsInfo(Map<String, Object> mapIn)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.bbcare.cp.plat.service.ISmsService#saveSmsInfoHis(java.util.Map)
	 */
	@Override
	public Map<String, Object> saveSmsInfoHis(Map<String, Object> mapIn)
			throws Exception {

		return null;
	}

}
